/*
 * Created by JFormDesigner on Tue Jul 02 10:48:35 CST 2024
 */

package scct.ZhuZhouXing.ui;

import java.awt.event.*;
import scct.ZhuZhouXing.dao.MembersDao;
import scct.ZhuZhouXing.model.Members;
import sun.applet.Main;

import java.awt.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 29414
 */
public class PaneMembersManager extends JPanel {
   public static   DefaultTableModel model;
    MainFrom main;
    public PaneMembersManager(MainFrom main) {
        this.main=main;
        initComponents();
        initView();//表提示文字
    }

    private void initView(){
    model = (DefaultTableModel)tabUser.getModel();
        model.addColumn("会员编号");
        model.addColumn("会员姓名");
        model.addColumn("性别");
        model.addColumn("账户");
        model.addColumn("密码");
        model.addColumn("会员类别");
        model.addColumn("手机号码");
        getTable();
    }

    public static void  getTable() {
        model.setRowCount(0);
        MembersDao membersDao = new MembersDao();
        List<Members> members = membersDao.gettallMembers();
        for (Members members1 : members) {
            Object[] row = {members1.getId(), members1.getUsername(), members1.getSex(), members1.getAccount(),
                    members1.getPassword(), members1.getMember(), members1.getNumber()};
            model.addRow(row);
        }
        model.fireTableDataChanged();//刷新界面显示数据
    }
//增加
    private void btnAdd(ActionEvent e) {
        DlgMembersAdd dlgMembersAdd=new DlgMembersAdd(main);
        dlgMembersAdd.setModal(true);
        dlgMembersAdd.setVisible(true);

    }
//修改
    private void btnUpdate(ActionEvent e) {
        if (tabUser.getSelectedRowCount() == 0){
            JOptionPane.showMessageDialog(this,"请你选择要修改的会员");
            return;
        }
        int index = tabUser.getSelectedRow();
        System.out.println(index +"测试===");
        int id = Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id + "测试===");
        DlgMembersupdDate dlgMembersupdDate=new DlgMembersupdDate(main,id);
        dlgMembersupdDate.setModal(true);
        dlgMembersupdDate.setVisible(true);

    }
//删除
    private void btndelect(ActionEvent e) {
    if (tabUser.getSelectedRowCount() == 0){
        JOptionPane.showMessageDialog(this,"请你选择要删除的会员");
        return;
    }
    int index = tabUser.getSelectedRow();
    System.out.println(index + "测试===");
    int id = Integer.parseInt(model.getValueAt(index,0).toString());
        System.out.println(id + "测试===");
        MembersDao membersDao=new MembersDao();
        if (membersDao.deMembers(id)){
            JOptionPane.showMessageDialog(this,"取消成功");
            PaneMembersManager.getTable();
        }
    }
         private void btnSelect(ActionEvent e) {
             model.setRowCount(0);//删除原有数据
             String name = txtquery.getText();//获取搜索框里面的值
             if(name.isEmpty()){
                 JOptionPane.showMessageDialog(this,"搜索框为空");
             }
             MembersDao membersDao=new MembersDao();
             List<Members>members=membersDao.getSelect(name);
             for (Members members1 : members) {
                 Object[] row = {members1.getId(), members1.getUsername(), members1.getSex(), members1.getAccount(),
                         members1.getPassword(), members1.getMember(), members1.getNumber()};
                 model.addRow(row);
             }
             model.fireTableDataChanged();//刷新界面显示数据
         }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 周文成
        panel2 = new JPanel();
        panel3 = new JPanel();
        txtquery = new JTextField();
        btnSelect = new JButton();
        comboBox1 = new JComboBox();
        scrollPane1 = new JScrollPane();
        tabUser = new JTable();
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        btndelect = new JButton();

        //======== this ========
        setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new javax. swing
        . border. EmptyBorder( 0, 0, 0, 0) , "JFor\u006dDesi\u0067ner \u0045valu\u0061tion", javax. swing. border. TitledBorder
        . CENTER, javax. swing. border. TitledBorder. BOTTOM, new java .awt .Font ("Dia\u006cog" ,java .
        awt .Font .BOLD ,12 ), java. awt. Color. red) , getBorder( )) )
        ;  addPropertyChangeListener (new java. beans. PropertyChangeListener( ){ @Override public void propertyChange (java .beans .PropertyChangeEvent e
        ) {if ("bord\u0065r" .equals (e .getPropertyName () )) throw new RuntimeException( ); }} )
        ;
        setLayout(new BorderLayout());

        //======== panel2 ========
        {
            panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));

            //======== panel3 ========
            {
                panel3.setLayout(new BoxLayout(panel3, BoxLayout.X_AXIS));
                panel3.add(txtquery);

                //---- btnSelect ----
                btnSelect.setText("\u67e5\u8be2");
                btnSelect.addActionListener(e -> btnSelect(e));
                panel3.add(btnSelect);
                panel3.add(comboBox1);
            }
            panel2.add(panel3);
        }
        add(panel2, BorderLayout.SOUTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabUser);
        }
        add(scrollPane1, BorderLayout.CENTER);

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u52a0\u5165\u4f1a\u5458");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539\u4f1a\u5458");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);

            //---- btndelect ----
            btndelect.setText("\u53d6\u6d88\u4f1a\u5458");
            btndelect.addActionListener(e -> btndelect(e));
            panel1.add(btndelect);
        }
        add(panel1, BorderLayout.NORTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 周文成
    private JPanel panel2;
    private JPanel panel3;
    private JTextField txtquery;
    private JButton btnSelect;
    private JComboBox comboBox1;
    private JScrollPane scrollPane1;
    private JTable tabUser;
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JButton btndelect;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
